package lovo01.modifyhomework;

import javax.swing.*;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * 管理系统主界面
 */
public class StudentUI {
    private Scanner sc = new Scanner(System.in);
    private SuperArray array = new SuperArray();

    /**
     * 菜单
     */
    public void menu(){
        JOptionPane.showMessageDialog(null,"欢迎来到学生管理系统");
        while(true){
            String input = JOptionPane.showInputDialog(null,"请选择：1-增加 2-修改 3-删除 4-查询 5-退出");
            if(input.equals("1")){
                insert();
            }else if(input.equals("2")){
                update();
            }else if(input.equals("3")){
                delete();
            }else if(input.equals("4")){
                search();
            }else if(input.equals("5")){
                JOptionPane.showMessageDialog(null,"Bye");
                System.exit(0);
            }else{
                JOptionPane.showMessageDialog(null,"输入有误，请重新输入");
            }
        }
    }

    /**
     * 增加
     */
    public void insert() {
        String name = JOptionPane.showInputDialog("请输入学生的姓名：");
        int age = 0;
            try {
                String age1 = JOptionPane.showInputDialog("请输入学生年龄：");
                age = Integer.parseInt(age1);
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null,"必须输入数字");
                return;
            }
        String gender = JOptionPane.showInputDialog("请输入学生的性别：");
            array.add(new Student(name, age, gender));
        JOptionPane.showMessageDialog(null,"增加成功");
    }

    /**
     * 修改
     */
    public void update(){
        String name = JOptionPane.showInputDialog("请输入要修改的学生姓名：");
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                String input = JOptionPane.showInputDialog("请选择要修改的内容：1-姓名 2-年龄 3-性别");
                Student stu = (Student) array.get(i);
                if(input.equals("1")){
                    String newName = JOptionPane.showInputDialog("请输入要修改的新姓名：");
                    stu.setName(newName);
                }else if(input.equals("2")){
                        int newAge = 0;
                        try {
                            String age1 = JOptionPane.showInputDialog("请输入修改的新年龄：");
                            newAge = Integer.parseInt(age1);
                        } catch (NumberFormatException e) {
                            JOptionPane.showMessageDialog(null,"必须输入数字");
                            return;
                        }
                        stu.setAge(newAge);
                }else if(input.equals("3")){
                    String newGender = JOptionPane.showInputDialog("请输入修改的新性别：");
                    stu.setGender(newGender);
                }else{
                    JOptionPane.showMessageDialog(null,"输入有误");
                    return;
                }
                JOptionPane.showMessageDialog(null,"修改完成");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }

    /**
     * 删除
     *
     */
    public void delete(){
        String name = JOptionPane.showInputDialog("请输入要删除的学生姓名：");
        for(int i = 0;i < array.size();i++){
            if(array.get(i).equals(new Student(name))){
                array.remove(i);
                JOptionPane.showMessageDialog(null,"删除成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"查无此人");
    }

    /**
     * 查询
     */
    public void search(){
        String input = JOptionPane.showInputDialog("请选择：1-查询所有 2-学生姓名");
        if(input.equals("1")){
            for(int i = 0;i < array.size();i++){
                JOptionPane.showMessageDialog(null,array.get(i));
            }
        }else{
            String name = JOptionPane.showInputDialog("请输入要查询的学生姓名：");
            boolean flag = false;
            for(int i = 0;i < array.size();i++){
                if(array.get(i).equals(new Student(name))){
                    JOptionPane.showMessageDialog(null,array.get(i));
                    flag = true;
                }
            }
            if(!flag){
                JOptionPane.showMessageDialog(null,"查无此人");
            }
        }
    }
}
